﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the App type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Windows;

    using Globals;
    using Resources;

    /// <summary>
    /// Defines the App type.
    /// </summary>
    public partial class App
    {
        /// <summary>
        /// Parameters list
        /// </summary>
        private IDictionary<string, string> parameters = new Dictionary<string, string>();

        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        /// <summary>
        /// Gets the current.
        /// </summary>
        public static new App Current
        {
            get { return Application.Current as App; }
        }

        /// <summary>
        /// Gets or sets the parameters.
        /// </summary>
        /// <value>
        /// The parameters.
        /// </value>
        public IDictionary<string, string> Parameters
        {
            get
            {
                return this.parameters ?? (this.parameters = new Dictionary<string, string>());
            }

            set
            {
                this.parameters = value;
            }
        }

        /// <summary>
        /// Reports the error to DOM.
        /// </summary>
        /// <param name="e">The <see cref="System.Windows.ApplicationUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private static void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch
            {
            }
        }

        // ReSharper disable InconsistentNaming

        /// <summary>
        /// Handles the Startup event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
        private void Application_Startup(object sender, StartupEventArgs e)

            // ReSharper restore InconsistentNaming
        {
            this.Parameters = e.InitParams;

            // TODO: remove this for StreetScene
            if (!SessionManager.Session.Contains("Language"))
            {
                StreetCare.Culture = new CultureInfo("es");
                SessionManager.Session.Add("Language", "es");
            }
            else
            {
                StreetCare.Culture = new CultureInfo(SessionManager.Session["Language"].ToString());
            }

            this.RootVisual = new MainPage();
        }

        // ReSharper disable MemberCanBeMadeStatic.Local
        // ReSharper disable InconsistentNaming

        /// <summary>
        /// Handles the Exit event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Application_Exit(object sender, EventArgs e)

            // ReSharper restore InconsistentNaming
            // ReSharper restore MemberCanBeMadeStatic.Local
        {
        }

        // ReSharper disable InconsistentNaming

        /// <summary>
        /// Handles the UnhandledException event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.ApplicationUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

            // ReSharper restore InconsistentNaming
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(() => ReportErrorToDOM(e));
            }
        }
    }
}
